﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acme.AuditExample.Business;
using Acme.AuditExample.Business.Objects;
using Acme.AuditExample.Business.Audit;

namespace AuditConsole
{
	class NHDALExample
	{
		public static void Run()
		{

			// Add a couple of customer objects. You will notice that during the creation of the customer collection 
			// we pass the modifier. We are also not setting modified_by, modified_on, created_by or created_on
			// fields. These are implemented by the framework.
			CustomerCollection customerCollection = new CustomerCollection("User14");

			//Create a simple customer
			//When persisted create record will be added to the audit table 
			Customer simpleCustomer = customerCollection.NewItem();
			simpleCustomer.Name = "Simple Customer";
			customerCollection.AddItem(simpleCustomer);

			//Create another customer
			//When persisted Create record will be added to the audit table
			Customer customer = customerCollection.NewItem();
			customer.Name = "Test Name 1";
			customerCollection.AddItem(customer);

			//Persist both customers they will both have the modifier or User14
			customerCollection.Persist();

			//Update the name. Updated record will be added to audit table
			customer.Name = "Test Name 2";
			customerCollection.Persist();

			//Lets look at what the create a modify produced
			//Retrieve customer from database that we just saved.
			Customer auditedCustomer = Customer.SelectUsingPK(customer.CustomerId, "User15");

			//Write Audit Records. There will be two records. 
			//The first record will represent the creation.
			//The second record will represent the modification.
			foreach (CustomerAudit customerAudit in auditedCustomer.GetAuditRecords())
			{
				Console.WriteLine("AuditDate: " + customerAudit.AuditDate.ToString());
				Console.WriteLine("AuditType: " + customerAudit.AuditType.ToString());
				Console.WriteLine("CustomerId: " + customerAudit.CustomerId.ToString());
				Console.WriteLine("Name: " + customerAudit.Name);
				Console.WriteLine("ModifiedBy: " + customerAudit.ModifiedBy);
			}

			//Modify the recored one more times and delete
			//We can then go to the database to view the full audit record
			Customer retrievedCustomer = Customer.SelectUsingPK(customer.CustomerId, "User15");

			//Update the name. Updated record will be added to audit table
			retrievedCustomer.Name = "Test Name 3";
			retrievedCustomer.Persist();

			//Delete the customer. Delte recored will be added to the audit table
			retrievedCustomer.Delete();
			retrievedCustomer.Persist();

		}
	}
}
